package cn.lili.modules.grab.dingdong;

import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.goods.entity.dos.StoreGoodsLabel;
import cn.lili.modules.grab.GrabGoodsParam;
import cn.lili.modules.grab.dingdong.facade.GrabDingdongGoodsFacade;
import cn.lili.modules.grab.dingdong.reps.goods.*;
import cn.lili.modules.grab.dingdong.res.goods.GoodsCateRequest;
import cn.lili.modules.grab.dingdong.res.goods.GoodsQueryRequest;
import cn.lili.modules.grab.dingdong.service.DingdongGoodsService;
import cn.lili.modules.grab.enums.SourceEnums;
import cn.lili.modules.grab.enums.StatusEnums;
import cn.lili.modules.grab.factory.goods.GrabGoodsService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class GrabDingdongGoodsServiceImpl implements GrabGoodsService {

    @Resource
    private DingdongGoodsService dingdongGoodsService;
    @Resource
    private GrabDingdongGoodsFacade grabDingdongGoodsFacade;

    @Override
    public SourceEnums step() {
        return SourceEnums.DINGDONG_GOODS;
    }

    /**
     * 商品列表
     *
     * @param grabParam 参数
     */
    @Override
    public ResultMessage<Object> goodsList(GrabGoodsParam grabParam) {
        log.info("企叮当--商品列表入参----{}", JSONObject.toJSONString(grabParam));
        List<GoodsQueryRespose> goodsQueryResposes = dingdongGoodsService.goodsList(grabParam.getGoodsQueryRequest());
        //其它业务逻辑
        if (goodsQueryResposes != null && !goodsQueryResposes.isEmpty()) {
            for (GoodsQueryRespose goodsQueryRespose : goodsQueryResposes) {
                try {
                    //查询商品详情=SKU
                    grabParam.getDetailRequest().setId(goodsQueryRespose.getGoods_id());
                    DetailRespose detailRespose = dingdongGoodsService.detail(grabParam.getDetailRequest());
                    //初始化商品 + SKU
                    grabDingdongGoodsFacade.goodsInit(grabParam.getStoreId(),goodsQueryRespose,detailRespose);
                }catch (Exception e){
                    log.error(e.getMessage());
                }
            }

            if(grabParam.getFlag()){
                //递归继续查找
                GoodsQueryRequest goodsQueryRequest = grabParam.getGoodsQueryRequest();
                goodsQueryRequest.setPage(String.valueOf(Integer.parseInt(goodsQueryRequest.getPage()) + 1));
                grabParam.setGoodsQueryRequest(goodsQueryRequest);
                goodsList(grabParam);
            }
        }
        return ResultUtil.data(StatusEnums.STATUS_200.getDes());
    }

    /**
     * 商品分类
     *
     * @param grabParam 参数
     */
    @Override
    public ResultMessage<Object> goodsCate(GrabGoodsParam grabParam) {
        log.info("企叮当--商品分类入参----{}", JSONObject.toJSONString(grabParam));
        //查询分类 并入库
        GoodsCateRequest goodsCateRequest = grabParam.getGoodsCateRequest();
        if(goodsCateRequest == null){
            goodsCateRequest = new GoodsCateRequest();
            goodsCateRequest.setParent_id("0");
        }
        List<GoodsCateRespose> goodsCateResposes = dingdongGoodsService.goodsCate(goodsCateRequest);
        if(goodsCateResposes == null){
            return ResultUtil.data(StatusEnums.STATUS_200.getDes());
        }
        for (GoodsCateRespose goodsCateRespose : goodsCateResposes) {
            grabDingdongGoodsFacade.storeGoodsLabelInit(grabParam.getStoreId(), goodsCateRespose);
            goodsCateRequest.setParent_id(goodsCateRespose.getCate_id());
            grabParam.setGoodsCateRequest(goodsCateRequest);
            goodsCate(grabParam);
        }
        return ResultUtil.data(StatusEnums.STATUS_200.getDes());
    }

    @Override
    public ResultMessage<Object> detail(GrabGoodsParam grabParam) {
        log.info("企叮当--商品配送方式入参----{}", JSONObject.toJSONString(grabParam));
        DetailRespose detailRespose = dingdongGoodsService.detail(grabParam.getDetailRequest());

        //其它业务逻辑
        return ResultUtil.data(StatusEnums.STATUS_200.getDes());
    }

    /**
     * 商品配送方式
     *
     * @param grabParam 参数
     */
    @Override
    public ResultMessage<Object> getYfMode(GrabGoodsParam grabParam) {
        log.info("企叮当--商品配送方式入参----{}", JSONObject.toJSONString(grabParam));
        GetYfModeRespose getYfModeRespose = dingdongGoodsService.getYfMode(grabParam.getGetYfModeRequest());
        //其它业务逻辑
        return ResultUtil.data(getYfModeRespose);
    }

    /**
     * 商品品牌列表
     *
     * @param grabParam 参数
     */
    @Override
    public ResultMessage<Object> getBrand(GrabGoodsParam grabParam) {
        log.info("企叮当--商品品牌列表入参----{}", JSONObject.toJSONString(grabParam));
        GetBrandRespose getBrandRespose = dingdongGoodsService.getBrand(grabParam.getGetBrandRequest());

        //其它业务逻辑
        return ResultUtil.data(StatusEnums.STATUS_200.getDes());
    }

    /**
     * 商品库存查询
     *
     * @param grabParam 参数
     */
    @Override
    public ResultMessage<Object> getGoodsStock(GrabGoodsParam grabParam) {
        log.info("企叮当--商品库存查询入参----{}", JSONObject.toJSONString(grabParam));
        GetGoodsStockRespose getGoodsStockRespose = dingdongGoodsService.getGoodsStock(grabParam.getGetGoodsStockRequest());

        //其它业务逻辑
        return ResultUtil.data(StatusEnums.STATUS_200.getDes());
    }
}
